<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像盒子 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" />
      </div>
      <!-- 登陆盒子 -->
      <el-form
        label-width="0px"
        class="login_from"
        :model="loginData"
        :rules="loginDataRules"
        ref="loginDataRef"
        v-on:keyup.native.enter="login"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="iconfont icon-user"
            v-model="loginData.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            prefix-icon="iconfont icon-lock_fill"
            v-model="loginData.password"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="button-from">
          <el-button type="primary" @click="login">登陆</el-button>
          <el-button type="info" @click="loginDataReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'app',
  data () {
    return {
      // 表单数据对象
      loginData: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则
      loginDataRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login () {
      this.$refs.loginDataRef.validate(async isPass => {
        if (!isPass) return
        const { data: resBody } = await this.$http.post('login', this.loginData)
        // 判断响应结果
        if (resBody.meta.status !== 200) return this.$msg.error(resBody.meta.msg)
        this.$msg.success(resBody.meta.msg)
        // 保存token令牌到内存中
        window.sessionStorage.setItem('token', resBody.data.token)
        this.$router.push('/home')
      })
    },
    loginDataReset () {
      this.$refs.loginDataRef.resetFields()
    }
  },
  component: {}
}
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background: url("../assets/微信图片_20210624160335.jpg") no-repeat center;
  color: #fff;
  background-size: cover;
}

.login_box {
  width: 450px;
  height: 300px;
  // background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #fff;
  }
}

.login_from {
  position: absolute;
  bottom: 0px;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.button-from {
  display: flex;
  justify-content: flex-end;
}
</style>
